package com.wbu.config;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisUtils {


    @Autowired
    private RedisTemplate redisTemplate;


    public boolean set(final String key,Object value){
        boolean result=false;
        ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();

        operations.set(key,value);
        result=true;
        return result;
    }

    public boolean set(final  String key, Object value, Long expireTime, TimeUnit timeUnit){
        boolean resule=false;

        ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
        operations.set(key,value);
        redisTemplate.expire(key,expireTime,timeUnit);
        return true;
    }

    public void remove(final String ... keys){
        for (String key : keys) {
            remove(key);
        }
    }
    public void removePattern(String pattern){
        Set<Serializable> keys = redisTemplate.keys(pattern);
         if(keys.size()>0){
             redisTemplate.delete(keys);
         }
    }
    public void remove(String key){
//        redisTempl
        if(exists(key)){
            redisTemplate.delete(key);
        }
    }

    public boolean exists(String key){
        return redisTemplate.hasKey(key);
    }

    public void add(String key, Object value){
        SetOperations<String ,Object> set = redisTemplate.opsForSet();
        set.add(key,value);
    }

    public Set<Object> getKeyLikePatern(String keyPattern){
        Set keys = redisTemplate.keys(keyPattern);
       if(!CollectionUtils.isEmpty(keys)){
           return keys;
       }else {
           return null;
       }
    }

    public <T> T get(String key){
        return (T)redisTemplate.opsForValue().get(key);
    }

    public static <T> String beanToString(T value){
        if(value==null){
            return null;
        }
        Class<?> clazz = value.getClass();
        if(clazz==int.class|| clazz==Integer.class){
            return String.valueOf(value);
        }else if(clazz==long.class|| clazz==Long.class){
            return String.valueOf(value);
        }else if(clazz==String.class){
            return (String) value;
        }else {
            return JSON.toJSONString(value);
        }
    }


    public static <T> T StringToBean(String str,Class<T> clazz){
        if(str==null||str.length()<=0||clazz==null){
            return null;
        }
        if(clazz==int.class|| clazz==Integer.class){
            return (T)Integer.valueOf(str);
        }else if(clazz==long.class|| clazz==Long.class){
            return (T)Long.valueOf(str);
        }else if(clazz==String.class){
            return (T) str;
        }else {
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }



}
